from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# 配置 DeepSeek（兼容 OpenAI 接口）
llm = ChatOpenAI(
    api_key="sk-48d8ce274c674dc68e7566f7ecea3476",
    base_url="https://api.deepseek.com",
    model="deepseek-chat"
)
# 定义问题
question = "请用中文解释一下机器学习中的超参数和欠拟合？"
# 系统消息
system_message = SystemMessage(content="你是一个知识丰富的讲解员，能够以通俗易懂的方式解释各种知识。")
# 用户消息
human_message = HumanMessage(content=question)


# 流式调用
def stream_chat():
    llm_response = llm.stream([
        system_message, human_message
    ])
    result = ""
    for chunk in llm_response:
        result += chunk.content
        print(chunk.content, end='', flush=True)
    return result


stream_chat()
